package cn.daniellee.plugin.ce.core;

import cn.daniellee.plugin.ce.CraftEconomy;
import cn.daniellee.plugin.ce.model.PlayerData;
import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class RankingCore {

	private static long lastRefresh;

	private static boolean sorting;

	private static List<PlayerData> ranking;

	private static Map<CommandSender, Integer> requesters = new ConcurrentHashMap<>();

	public static void sendRanking(CommandSender sender, int page) {
		if (sorting) {
			sender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.balance-sorting", "&eSorting all players' balances, please wait...")).replace("&", "§"));
			requesters.put(sender, page);
		} else {
			int refreshTimeout = CraftEconomy.getInstance().getConfig().getInt("setting.ranking-validity", 300);
			if (System.currentTimeMillis() - lastRefresh > refreshTimeout * 1000) {
				sorting = true;
				requesters.put(sender, page);
				sender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.balance-sorting", "&eSorting all players' balances, please wait...")).replace("&", "§"));
				new BukkitRunnable() {
					@Override
					public void run() {
						List<PlayerData> allPlayerData = CraftEconomy.getInstance().getStorage().getAllPlayerData();
						for (int i = 0; i < allPlayerData.size() - 1; i++) {
							for (int j = i + 1; j < allPlayerData.size(); j++) {
								PlayerData a = allPlayerData.get(i);
								PlayerData b = allPlayerData.get(j);
								if (a.getBalance() < b.getBalance()) {
									allPlayerData.set(i, b);
									allPlayerData.set(j, a);
								}
							}
						}
						sorting = false;
						ranking = allPlayerData;
						lastRefresh = System.currentTimeMillis();
						for (Map.Entry<CommandSender, Integer> requester : requesters.entrySet()) {
							sendRanking(requester.getKey(), requester.getValue());
						}
						requesters.clear();
					}
				}.runTaskAsynchronously(CraftEconomy.getInstance());
			} else {
				String rankingFormat = CraftEconomy.getInstance().getConfig().getString("message.ranking-format", "&e{place}: {player} - {balance}");
				int pageStart = (page - 1) * 10;
				int pageEnd = pageStart + 10;
				List<PlayerData> pageData = ranking.subList(Math.min(pageStart, ranking.size()), Math.min(pageEnd, ranking.size()));
				if (pageData.size() > 0) {
					for (int i = 0; i < pageData.size(); i++) {
						sender.sendMessage((CraftEconomy.getInstance().getPrefix() + rankingFormat.replace("{place}", Integer.toString(pageStart + i + 1)).replace("{player}", pageData.get(i).getName()).replace("{balance}", CraftEconomy.getInstance().getFormat().format(pageData.get(i).getBalance()))).replace("&", "§"));
					}
				} else sender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.invalid-page", "&eThe page number is invalid, please re-enter.")).replace("&", "§"));
			}
		}
	}
}
